package com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.entity.ErpFinanceincome;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.entity.ErpFinanceincomebill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.mapper.ErpFinanceincomebillMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.dto.ErpFinanceincomeListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.mapper.ErpFinanceincomeMapper;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceIncome.service.IErpFinanceincomeService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-09-23
 */
@Service
@AllArgsConstructor
public class ErpFinanceincomeServiceImpl extends BaseService<ErpFinanceincomeMapper, ErpFinanceincome> implements IErpFinanceincomeService {

	private ErpFinanceincomebillMapper erpFinanceincomebillMapper;

	@Override
	public IPage<ErpFinanceincome> getPageList(ErpFinanceincomeListDto pageListDto) {
		Wrapper<ErpFinanceincome> wrapper = Wrappers.<ErpFinanceincome>query().lambda()				.like(!StringUtil.isEmpty(pageListDto.getF_IncomeCode()), ErpFinanceincome::getFIncomecode, pageListDto.getF_IncomeCode())
				.like(!StringUtil.isEmpty(pageListDto.getF_Manager()), ErpFinanceincome::getFManager, pageListDto.getF_Manager())
				.eq(!StringUtil.isEmpty(pageListDto.getF_BusinessType()), ErpFinanceincome::getFBusinesstype, pageListDto.getF_BusinessType());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpFinanceincome(ErpFinanceincome erpFinanceincome, List<ErpFinanceincomebill> erpFinanceincomebillList) {
		String erpFinanceincomeId = IdWorker.get32UUID();
		erpFinanceincome.setFId(erpFinanceincomeId);
		erpFinanceincomebillList.forEach(erpFinanceincomebill -> erpFinanceincomebill.setFIncomeid(erpFinanceincomeId));
		this.saveBatch(erpFinanceincomebillList, ErpFinanceincomebill.class, ErpFinanceincomebillMapper.class);
		boolean isSuccess = this.save(erpFinanceincome);
		return isSuccess ? erpFinanceincomeId : null;
	}

	@Override
	public boolean updateErpFinanceincome(String id, ErpFinanceincome erpFinanceincome, List<ErpFinanceincomebill> erpFinanceincomebillList) {
		erpFinanceincome.setFId(id);
		erpFinanceincomebillList.forEach(erpFinanceincomebill -> erpFinanceincomebill.setFIncomeid(id));
		erpFinanceincomebillMapper.delete(Wrappers.<ErpFinanceincomebill>query().lambda().eq(ErpFinanceincomebill::getFIncomeid, id));
		this.saveBatch(erpFinanceincomebillList, ErpFinanceincomebill.class, ErpFinanceincomebillMapper.class);
		return this.updateById(erpFinanceincome);
	}

	public List<ErpFinanceincomebill> getErpFinanceincomebillByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpFinanceincomebill>query().lambda().eq(ErpFinanceincomebill::getFIncomeid, parentId);
		return erpFinanceincomebillMapper.selectList(wrapper);
	}
}
